import datetime
from sanic import response

from model.deviceModel import search_contract_link_dev, insert_contract_link_dev, search_dev, judge_a_device, \
        update_dev, delete_contract_link_dev


def get_contract_dev_list(contract_id):
        dev_list = search_contract_link_dev(contract_id=contract_id)
        return dev_list


# 合同设备列表
async def contract_dev(request):
        contract_id = request.form.get('contract_id')

        contract_dev_info = get_contract_dev_list(contract_id=contract_id)      # 合同设备信息
        devID_list = [i['device_id'] for i in contract_dev_info]        # 设备ID
        dev_info = search_dev(device_id=devID_list)     # 设备信息

        dev = []        # 返回的设备信息
        temp = {'deviceSN': None, 'bind_time': None}
        for i in range(len(contract_dev_info)):
                # 设备号
                temp['deviceSN'] = dev_info[i]['deviceSN']
                # 合同添加设备时间
                temp['bind_time'] = datetime.date.isoformat(contract_dev_info[i]['bind_time'])  # 取出datetime并转化
                dev.append(temp)
        return response.json(dev)


# 添加合同设备
async def contract_add_dev(request):
        contract_id = request.form.get('contract_id')
        device_id = request.form.get('device_id')
        addname = request.form.get('name')

        bingo = judge_a_device(status='B', device_id=device_id)
        if bingo:
                print('设备符合')
                # 1.插入关系
                # 2.修改设备状态
                success1 = insert_contract_link_dev(contract_id=contract_id, device_id=device_id, addname=addname)
                success2 = update_dev(status='C', device_id=device_id)
                if success1 and success2:
                        return response.text('添加成功！')
                else:
                        # 回滚
                        delete_contract_link_dev(contract_id=contract_id, device_id=device_id)
                        update_dev(status='B', device_id=device_id)
                        return response.text('添加失败！')
        else:
                return response.text('添加失败：设备不符合!')


async def contract_delete_dev(request):
        contract_id = request.form.get('contract_id')
        device_id = request.form.get('device_id')

        bingo = judge_a_device(status='C', device_id=device_id)
        if bingo:
                # 该状态
                success2 = update_dev(status='A', device_id=device_id)
                if success2:
                        # 删除关系
                        success1 = delete_contract_link_dev(contract_id=contract_id, device_id=device_id)
                        if success1:
                                return response.text('删除成功！')
                        else:
                                return response.text('删除失败！')
                else:
                        # 回滚
                        update_dev(status='C', device_id=device_id)     # <<<<<<<<<<<<<<<<<删除没法回滚？？？
                        return response.text('删除失败！')
        else:
                return response.text('删除失败！')




